// B-OK Analysis
// Aaron Richterman
// April 25, 2023


//set up global paths
global dat "/Users/aarichter/Desktop/Research projects/B-ok analysis"

//load survey dataset and view it
clear
use "$dat/survey_dataset.dta"

sum

//recode race

gen race_black=.
replace race_black = 1 if race___1 == 1
replace race_black = 0 if race___1 == 0
tab race_black

gen race_white=.
replace race_white = 1 if race___2 == 1
replace race_white = 0 if race___2 == 0
tab race_white

gen race_asian=.
replace race_asian = 1 if race___3 == 1
replace race_asian = 0 if race___3 == 0
tab race_asian

gen race_na=.
replace race_na = 1 if race___4 == 1
replace race_na = 0 if race___4 == 0
tab race_na

gen race_other=.
replace race_other = 1 if race___6 == 1 | race___9 == 1
replace race_other = 0 if race___6 == 0 & race___9 == 0
tab race_other

gen race_cat=.
//non-hispanic
replace race_cat = 1 if race_black == 1
replace race_cat = 2 if race_white == 1
replace race_cat = 3 if race_asian == 1
replace race_cat = 4 if race_na == 1
replace race_cat = 5 if race_other == 1
replace race_cat = 6 if (race_black+race_white+race_asian+race_na+race_other) > 1
//hispanic
replace race_cat = 7 if ethnicity == 1 

tab race_cat

//recode gender

gen gender_cat=.
replace gender_cat = 1 if female_sex == gender
replace gender_cat = 2 if female_sex != gender
tab gender_cat

//recode ART rx

gen art_yn=.
replace art_yn = 1 if art1___1 == 1 | art1___2 == 1
replace art_yn = 0 if art1___3 == 1
tab art_yn

gen art_cat=.
replace art_cat = 1 if art1___3 == 1 // no ART
replace art_cat = 2 if art1___2 == 1 // injectable ART
replace art_cat = 3 if art1___1 == 1 // oral ART
tab art_cat

//recode ART adherence

sum art2, detail

// above and below median adherence

gen high_adherence=.
replace high_adherence=.
replace high_adherence = 1 if art2 == 0 
replace high_adherence = 0 if art2 >0
replace high_adherence=. if art2==.
replace high_adherence = 0 if art1___3 == 1
tab high_adherence

gen optimal_adherence=.
replace optimal_adherence=.
replace optimal_adherence = 1 if art2 == 0 | art2 == 1 
replace optimal_adherence = 0 if art2 >1
replace optimal_adherence=. if art2==.
replace optimal_adherence = 0 if art1___3 == 1
tab optimal_adherence


gen high80_adherence=.
replace high80_adherence=.
replace high80_adherence = 1 if art2 > 25
replace high80_adherence = 0 if art2 < 26
replace high80_adherence=. if art2==.
replace high80_adherence = 0 if art1___3 == 1
tab high80_adherence

// interview category variable

gen interview_cat = .
replace interview_cat = 1 if pre_post == 1 // pre-interview, to exclude
replace interview_cat = 2 if pre_post == 2 // post-interview
replace interview_cat = 3 if pre_post == . // no interview
tab interview_cat

gen pre_interview = .
replace pre_interview = 1 if pre_post == 1
replace pre_interview = 0 if pre_post == . | pre_post == 2

// descriptive characteristics table

sum age, detail
tab female_sex
tab gender_cat
tab language
tab race_cat
tab partner_site
tab art_cat
sum art2, detail
tab interview_cat

// awareness variables (binary)

// awareness of viral suppression
tab awareness1a_pre 
tab awareness1a_post

// recode variables to 0/1, comparison to generate p-value
gen awareness1a_bin_pre=.
replace awareness1a_bin_pre = 1 if awareness1a_pre == 1
replace awareness1a_bin_pre = 0 if awareness1a_pre == 2 | awareness1a_pre == 3
tab awareness1a_bin_pre awareness1a_pre

gen awareness1a_bin_post=.
replace awareness1a_bin_post = 1 if awareness1a_post == 1
replace awareness1a_bin_post = 0 if awareness1a_post == 2 | awareness1a_post == 3
tab awareness1a_bin_post awareness1a_post

prtest awareness1a_bin_pre == awareness1a_bin_post

// awareness of U=U
tab awareness2a_pre 
tab awareness2a_post

// recode variables to 0/1, comparison to generate p-value
gen awareness2a_bin_pre=.
replace awareness2a_bin_pre = 1 if awareness2a_pre == 1
replace awareness2a_bin_pre = 0 if awareness2a_pre == 2 | awareness2a_pre == 3
tab awareness2a_bin_pre awareness2a_pre

gen awareness2a_bin_post=.
replace awareness2a_bin_post = 1 if awareness2a_post == 1
replace awareness2a_bin_post = 0 if awareness2a_post == 2 | awareness2a_post == 3
tab awareness2a_bin_post awareness2a_post

prtest awareness2a_bin_pre == awareness2a_bin_post

// OUTCOMES
//
//


/* 
Following the binary awareness variable, there is a second 0-10 question
asked only to those who reported having heard of the terms.
Below generates another variable that recodes everyone who answered No
to the first question to a 0 understanding
*/ 
clear
use "$dat/survey_dataset_recode.dta" 

tab awareness1b_pre
tab awareness1b_post
tab awareness2b_pre
tab awareness2b_post

gen awareness1b_all_pre=awareness1b_pre
replace awareness1b_all_pre=0 if awareness1a_bin_pre == 0
tab awareness1b_all_pre

gen awareness1b_all_post=awareness1b_post
replace awareness1b_all_post=0 if awareness1a_bin_post == 0
tab awareness1b_all_post

gen awareness2b_all_pre=awareness2b_pre
replace awareness2b_all_pre=0 if awareness2a_bin_pre == 0
tab awareness2b_all_pre

gen awareness2b_all_post=awareness2b_post
replace awareness2b_all_post=0 if awareness2a_bin_post == 0
tab awareness2b_all_post

// generate difference variables post-pre 

gen awareness1b_diff=awareness1b_all_post-awareness1b_all_pre
gen awareness2b_diff=awareness2b_all_post-awareness2b_all_pre
gen knowledge1_diff=knowledge1_post-knowledge1_pre
gen knowledge2_diff=knowledge2_post-knowledge2_pre
gen attitude1_diff=attitude_1_post-attitude1_pre
gen attitude2_diff=attitude_2_post-attitude2_pre
gen intention1_diff=intention1_post-intention1_pre
gen intention2_diff=intention_2_post-intention2_pre
gen perception_diff=perception_post-perception_pre

// histograms of baseline
histogram awareness1b_all_pre
histogram awareness2b_all_pre
histogram knowledge1_pre
histogram knowledge2_pre
histogram attitude1_pre
histogram attitude2_pre
histogram intention1_pre
histogram intention2_pre
histogram perception_pre

// histograms of differences

histogram awareness1b_diff
histogram awareness2b_diff
histogram knowledge1_diff
histogram knowledge2_diff
histogram attitude1_diff
histogram attitude2_diff
histogram intention1_diff
histogram intention2_diff
histogram perception_diff

// baseline values

sum awareness1b_all_pre, detail
sum awareness2b_all_pre, detail
sum knowledge1_pre, detail
sum knowledge2_pre, detail
sum attitude1_pre, detail
sum attitude2_pre, detail
sum intention1_pre, detail
sum intention2_pre, detail
sum perception_pre, detail

// post-intervention values

sum awareness1b_all_post, detail
sum awareness2b_all_post, detail
sum knowledge1_post, detail
sum knowledge2_post, detail
sum attitude_1_post, detail
sum attitude_2_post, detail
sum intention1_post, detail
sum intention_2_post, detail
sum perception_post, detail


// differences

sum awareness1b_diff, detail
sum awareness2b_diff, detail
sum knowledge1_diff, detail
sum knowledge2_diff, detail
sum attitude1_diff, detail
sum attitude2_diff, detail
sum intention1_diff, detail
sum intention2_diff, detail
sum perception_diff, detail

// p-values for differences

ttest awareness1b_diff == 0
ttest awareness2b_diff == 0
ttest knowledge1_diff == 0
ttest knowledge2_diff == 0
ttest attitude1_diff == 0
ttest attitude2_diff == 0
ttest intention1_diff == 0
ttest intention2_diff == 0
ttest perception_diff == 0

//non-parametric
signrank awareness1b_diff = 0
signrank awareness2b_diff = 0
signrank knowledge1_diff = 0
signrank knowledge2_diff = 0
signrank attitude1_diff = 0
signrank attitude2_diff = 0
signrank intention1_diff = 0
signrank intention2_diff = 0
signrank perception_diff = 0

// Outcomes in sub-groups
//
//
//
//
//

//pre- vs post-intervention interviews

// differences
sort pre_interview
by pre_interview: sum awareness1b_diff, detail
by pre_interview: sum awareness2b_diff, detail
by pre_interview: sum knowledge1_diff, detail
by pre_interview: sum knowledge2_diff, detail
by pre_interview: sum attitude1_diff, detail
by pre_interview: sum attitude2_diff, detail
by pre_interview: sum intention1_diff, detail
by pre_interview: sum intention2_diff, detail
by pre_interview: sum perception_diff, detail

// p-values for differences by pre- vs post-interview

ttest awareness1b_diff, by(pre_interview)
ttest awareness2b_diff, by(pre_interview)
ttest knowledge1_diff, by(pre_interview)
ttest knowledge2_diff, by(pre_interview)
ttest attitude1_diff, by(pre_interview)
ttest attitude2_diff, by(pre_interview)
ttest intention1_diff, by(pre_interview)
ttest intention2_diff, by(pre_interview)
ttest perception_diff, by(pre_interview)

//high- vs low-adherence

// differences
sort high_adherence
by high_adherence: sum awareness1b_diff, detail
by high_adherence: sum awareness2b_diff, detail
by high_adherence: sum knowledge1_diff, detail
by high_adherence: sum knowledge2_diff, detail
by high_adherence: sum attitude1_diff, detail
by high_adherence: sum attitude2_diff, detail
by high_adherence: sum intention1_diff, detail
by high_adherence: sum intention2_diff, detail
by high_adherence: sum perception_diff, detail

// p-values for differences by high vs low adherence

ttest awareness1b_diff, by(high_adherence)
ttest awareness2b_diff, by(high_adherence)
ttest knowledge1_diff, by(high_adherence)
ttest knowledge2_diff, by(high_adherence)
ttest attitude1_diff, by(high_adherence)
ttest attitude2_diff, by(high_adherence)
ttest intention1_diff, by(high_adherence)
ttest intention2_diff, by(high_adherence)
ttest perception_diff, by(high_adherence)

//optimal vs suboptimal adherence

// differences
sort optimal_adherence
by optimal_adherence: sum awareness1b_diff, detail
by optimal_adherence: sum awareness2b_diff, detail
by optimal_adherence: sum knowledge1_diff, detail
by optimal_adherence: sum knowledge2_diff, detail
by optimal_adherence: sum attitude1_diff, detail
by optimal_adherence: sum attitude2_diff, detail
by optimal_adherence: sum intention1_diff, detail
by optimal_adherence: sum intention2_diff, detail
by optimal_adherence: sum perception_diff, detail

// p-values for differences by optimal vs low adherence

ttest awareness1b_diff, by(optimal_adherence)
ttest awareness2b_diff, by(optimal_adherence)
ttest knowledge1_diff, by(optimal_adherence)
ttest knowledge2_diff, by(optimal_adherence)
ttest attitude1_diff, by(optimal_adherence)
ttest attitude2_diff, by(optimal_adherence)
ttest intention1_diff, by(optimal_adherence)
ttest intention2_diff, by(optimal_adherence)
ttest perception_diff, by(optimal_adherence)

//CBO (Bebashi) vs university site (Drexel, Temple, Einstein)

gen cbo=0
replace cbo=1 if partner_site == 1

// differences
sort cbo
by cbo: sum awareness1b_diff, detail
by cbo: sum awareness2b_diff, detail
by cbo: sum knowledge1_diff, detail
by cbo: sum knowledge2_diff, detail
by cbo: sum attitude1_diff, detail
by cbo: sum attitude2_diff, detail
by cbo: sum intention1_diff, detail
by cbo: sum intention2_diff, detail
by cbo: sum perception_diff, detail

// p-values for differences by community vs clinic

ttest awareness1b_diff, by(cbo)
ttest awareness2b_diff, by(cbo)
ttest knowledge1_diff, by(cbo)
ttest knowledge2_diff, by(cbo)
ttest attitude1_diff, by(cbo)
ttest attitude2_diff, by(cbo)
ttest intention1_diff, by(cbo)
ttest intention2_diff, by(cbo)
ttest perception_diff, by(cbo)

//LAI vs oral ART

gen lai=0
replace lai=1 if art_cat == 2
replace lai=. if art_cat == 1
tab lai

// differences
sort lai
by lai: sum awareness1b_diff, detail
by lai: sum awareness2b_diff, detail
by lai: sum knowledge1_diff, detail
by lai: sum knowledge2_diff, detail
by lai: sum attitude1_diff, detail
by lai: sum attitude2_diff, detail
by lai: sum intention1_diff, detail
by lai: sum intention2_diff, detail
by lai: sum perception_diff, detail


// p-values for baseline differences by LAI

ttest awareness1b_all_post, by(lai)
ttest awareness2b_all_post, by(lai)
ttest knowledge1_post, by(lai)
ttest knowledge2_post, by(lai)
ttest attitude_1_post, by(lai)
ttest attitude_2_post, by(lai)
ttest intention1_post, by(lai)
ttest intention_2_post, by(lai)
ttest perception_post, by(lai)
 
// p-values for differences by LAI

ttest awareness1b_diff, by(lai)
ttest awareness2b_diff, by(lai)
ttest knowledge1_diff, by(lai)
ttest knowledge2_diff, by(lai)
ttest attitude1_diff, by(lai)
ttest attitude2_diff, by(lai)
ttest intention1_diff, by(lai)
ttest intention2_diff, by(lai)
ttest perception_diff, by(lai)


// Implementation outcome scales

gen aim_tot = (aim1+aim2+aim3+aim4)/4
gen fim_tot = (fim1+fim2+fim3+fim4)/4
gen iam_tot = (iam1+iam2+iam3+iam4)/4

sum aim1, detail
sum aim2, detail
sum aim3, detail
sum aim4, detail
sum aim_tot, detail
sum fim1, detail
sum fim2, detail
sum fim3, detail
sum fim4, detail
sum fim_tot, detail
sum iam1, detail
sum iam2, detail
sum iam3, detail
sum iam4, detail
sum iam_tot, detail

// Participant interaction with bottles

// generate variables for interactions at any point during the session
clear
use "$dat/survey_dataset_recode.dta"

gen fidelity1_overall=0
replace fidelity1_overall=1 if (fidelity1___1==1 | fidelity1___2==1 | fidelity1___3==1)
tab fidelity1_overall

gen fidelity2_overall=0
replace fidelity2_overall=1 if (fidelity2___1==1 | fidelity2___2==1 | fidelity2___3==1)
tab fidelity2_overall

gen fidelity3_overall=0
replace fidelity3_overall=1 if (fidelity3___1==1 | fidelity3___2==1 | fidelity3___3==1)
tab fidelity3_overall

gen fidelity4_overall=0
replace fidelity4_overall=1 if (fidelity4___1==1 | fidelity4___2==1 | fidelity4___3==1)
tab fidelity4_overall

gen fidelity5_overall=0
replace fidelity5_overall=1 if (fidelity5___1==1 | fidelity5___2==1 | fidelity5___3==1)
tab fidelity5_overall

// summary statistics for interactions

tab fidelity1___1
tab fidelity2___1
tab fidelity3___1
tab fidelity4___1
tab fidelity5___1

tab fidelity1___2
tab fidelity2___2
tab fidelity3___2
tab fidelity4___2
tab fidelity5___2

tab fidelity1___3
tab fidelity2___3
tab fidelity3___3
tab fidelity4___3
tab fidelity5___3

tab fidelity1_overall
tab fidelity2_overall
tab fidelity3_overall
tab fidelity4_overall
tab fidelity5_overall


// Process analysis

//load process dataset and view it
clear
use "$dat/team_forms_wide.dta"

//drop the duplicates/excluded as these will be considered screened but not completed

// participant 13 — did not complete survey

replace study_id1 = . if study_id1 == 13
replace study_id2 = . if study_id2 == 13
replace study_id3 = . if study_id3 == 13
replace study_id4 = . if study_id4 == 13
replace study_id5 = . if study_id5 == 13
replace study_id6 = . if study_id6 == 13

// participant 80 — participant was on PrEP, not HIV+, not eligible for study

replace study_id1 = . if study_id1 == 80
replace study_id2 = . if study_id2 == 80
replace study_id3 = . if study_id3 == 80
replace study_id4 = . if study_id4 == 80
replace study_id5 = . if study_id5 == 80
replace study_id6 = . if study_id6 == 80

// participant 68 - duplicate; person completed survey 2x 
// of the 4, this is the only survey that was actually recorded in this 
// dataset (under study_id3), therefore need to decrease survey_outcome by 1

replace survey_outcome=survey_outcome-1 if study_id3 == 68

replace study_id1 = . if study_id1 == 68
replace study_id2 = . if study_id2 == 68
replace study_id3 = . if study_id3 == 68
replace study_id4 = . if study_id4 == 68
replace study_id5 = . if study_id5 == 68
replace study_id6 = . if study_id6 == 68

// participant 115 - duplicate; person completed survey 2x 

replace study_id1 = . if study_id1 == 115
replace study_id2 = . if study_id2 == 115
replace study_id3 = . if study_id3 == 115
replace study_id4 = . if study_id4 == 115
replace study_id5 = . if study_id5 == 115
replace study_id6 = . if study_id6 == 115

// Screening
sum screening_outcome
display r(sum)
sort partner_site
by partner_site: egen sum_screening = total(screening_outcome)
by partner_site: sum sum_screening

// Enrolled
sum survey_outcome
display r(sum)
sort partner_site
by partner_site: egen sum_enrolled = total(survey_outcome)
by partner_site: sum sum_enrolled

// Enrolled / person-day on site
tab partner_site

//
//
// Generating indicator variables for qualitative interviews
// Above or below median for changes in attitude and intentions related to TasP
// 
//

gen high_attitude1_diff = .
replace high_attitude1_diff = 1 if attitude1_diff<0
replace high_attitude1_diff = 0 if attitude1_diff>=0
tab high_attitude1_diff

gen high_intention1_diff = .
replace high_intention1_diff = 1 if intention1_diff<0
replace high_intention1_diff = 0 if intention1_diff>=0
tab high_intention1_diff

use if interview_cat == 1 | interview_cat == 2 using "$dat/survey_dataset_recode.dta", clear
use "$dat/survey_dataset_recode.dta", clear

//
//
//
//
//
// FOR THE INTERVIEWED SUBSET, DESCRIPTIVES FOR IMPLEMENTATION PAPER
//
//
//
//
clear
use if interview_cat==1 | interview_cat==2 using "$dat/survey_dataset_recode.dta" 

sum age, detail
tab female_sex
tab gender_cat
tab language
tab race_cat
tab partner_site
tab art_cat
sum art2, detail
tab interview_cat

// Implementation outcome scales

sum aim1, detail
sum aim2, detail
sum aim3, detail
sum aim4, detail
sum aim_tot, detail
sum fim1, detail
sum fim2, detail
sum fim3, detail
sum fim4, detail
sum fim_tot, detail
sum iam1, detail
sum iam2, detail
sum iam3, detail
sum iam4, detail
sum iam_tot, detail

// summary statistics for interactions

tab fidelity1___1
tab fidelity2___1
tab fidelity3___1
tab fidelity4___1
tab fidelity5___1

tab fidelity1___2
tab fidelity2___2
tab fidelity3___2
tab fidelity4___2
tab fidelity5___2

tab fidelity1___3
tab fidelity2___3
tab fidelity3___3
tab fidelity4___3
tab fidelity5___3

tab fidelity1_overall
tab fidelity2_overall
tab fidelity3_overall
tab fidelity4_overall
tab fidelity5_overall


